//
//  PkViewController.m
//  Learn
//
//  Created by apple on 2024/4/6.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "PkViewController.h"
#import "PkUserInfoView.h"
#import "PKScanView.h"
#import "PKMatchView.h"
#import "PKDatiView.h"
#import "PKResultView.h"

@interface PkViewController ()
@property (nonatomic, strong) PkMatchModel    * model;
@property (nonatomic, strong) UIImageView    * bgImg;
@property (nonatomic, strong) PkUserInfoView    * infoView;
@property (nonatomic, strong) UIView     * scanBgView;

@property (nonatomic, strong) PKScanView    * scanView;
@property (nonatomic, strong) UIImageView     * userHeaderImg;

@property (nonatomic, strong) PKMatchView    * matchView;

@property (nonatomic, strong) PKDatiView    * datiView;
@property (nonatomic, strong) PKResultView    * resultView;


@end

@implementation PkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.bgImg];

    [self initCusNav];
    self.navigationBar.navTitle.text = @"单词PK";
    self.navigationBar.navTitle.textColor = UIColor.whiteColor;
    self.navigationBar.leftImage.image = [UIImage imageNamed:@"icon_back_white"];
    self.navigationBar.bgColor = [UIColor.whiteColor colorWithAlphaComponent:0];
    [self initUI];
    // Do any additional setup after loading the view.
}
- (UIImageView *)bgImg {
    if(!_bgImg){
        _bgImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W,SCREEN_H)];
        _bgImg.contentMode = UIViewContentModeScaleAspectFill;
        _bgImg.image = [UIImage imageNamed:@"pk_bg"];
    }
    return _bgImg;
}
- (void)initUI {
    [self.view addSubview:self.infoView];
    [self.view addSubview:self.scanBgView];
    [self.view addSubview:self.matchView];
    [self.view addSubview:self.datiView];
    [self.view addSubview:self.resultView];
}
- (PkUserInfoView *)infoView {
    if (!_infoView) {
        _infoView = [[PkUserInfoView alloc]initWithFrame:CGRectMake(0, NavigationBarHeight + 20, SCREEN_W, 40 + 1.26*(SCREEN_W - 36))];
        WS(WeakSelf)
        _infoView.pkClickedBlock = ^(PkMatchModel * _Nonnull model) {
            WeakSelf.model = model;
            [WeakSelf startScan];
            [WeakSelf.matchView updateUIWithModel:model];
            [WeakSelf.datiView updateUIWithModel:model];
            [WeakSelf.resultView updateUIWithModel:model];
        };
    }
    return _infoView;
}

- (UIView *)scanBgView {
    if(!_scanBgView){
        _scanBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 60 + NavigationBarHeight, SCREEN_W, SCREEN_W + 80)];
        
        _scanBgView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(kWIDTH_RATIO*39, 20, SCREEN_W - kWIDTH_RATIO*78, SCREEN_W - kWIDTH_RATIO*78)];
        [lineView js_setRoundedCorners:UIRectCornerAllCorners radii:(SCREEN_W - kWIDTH_RATIO*78)/2 borderColor:UIColor.whiteColor width:0.5];
        [_scanBgView addSubview:lineView];
        
        
        UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectMake(kWIDTH_RATIO*69, 20, SCREEN_W - kWIDTH_RATIO*138, SCREEN_W - kWIDTH_RATIO*138)];
        lineView1.center = lineView.center;
        lineView1.backgroundColor = [UIColor.whiteColor colorWithAlphaComponent:0.15];
        [lineView1 js_setRoundedCorners:UIRectCornerAllCorners radii:(SCREEN_W - kWIDTH_RATIO*138)/2];
        [_scanBgView addSubview:lineView1];
        
        UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(kWIDTH_RATIO*124, 20, SCREEN_W - kWIDTH_RATIO*248, SCREEN_W - kWIDTH_RATIO*248)];
        lineView2.center = lineView.center;
        lineView2.backgroundColor = [UIColor.whiteColor colorWithAlphaComponent:0.2];
        [lineView2 js_setRoundedCorners:UIRectCornerAllCorners radii:(SCREEN_W - kWIDTH_RATIO*248)/2];
        [_scanBgView addSubview:lineView2];
        
        
        _scanBgView.hidden = YES;
        [_scanBgView addSubview:self.scanView];
        self.scanView.center = lineView.center;
        [_scanBgView addSubview:self.userHeaderImg];
        self.userHeaderImg.center = lineView.center;

        [self addUserHeadeWithBottom:79 leftSpace:26];
        [self addUserHeadeWithBottom:-17 leftSpace:121];

        [self addUserHeadeWithBottom:70 leftSpace:-110];
        [self addUserHeadeWithBottom:-176 leftSpace:65];
        [self addUserHeadeWithBottom:-104 leftSpace:-167];

        UILabel *tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_W, SCREEN_W, 28)];
        tipLabel.text = @"寻找对局…";
        tipLabel.textColor = UIColor.whiteColor;
        tipLabel.font = PingFangMediumFont(20);
        tipLabel.textAlignment = NSTextAlignmentCenter;
        [_scanBgView addSubview:tipLabel];
    }
    return _scanBgView;
}

- (PKScanView *)scanView {
    if (!_scanView) {
        _scanView = [PKScanView scanRadarViewWithRadius:0.63*SCREEN_W*0.5 angle:200 radarLineNum:0 hollowRadius:kWIDTH_RATIO*40];
    }
    return _scanView;
}
- (UIImageView *)userHeaderImg {
    if(!_userHeaderImg){
        _userHeaderImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWIDTH_RATIO*80,kWIDTH_RATIO*80)];
        _userHeaderImg.contentMode = UIViewContentModeScaleAspectFill;
        [_userHeaderImg sd_setImageWithURL:[NSURL URLWithString:lApp.useModel.avatar] placeholderImage:[UIImage imageNamed:@"testheader"]];
        [_userHeaderImg js_setRoundedCorners:UIRectCornerAllCorners radii:kWIDTH_RATIO*40 borderColor:UIColor.whiteColor width:1];
    }
    return _userHeaderImg;
}

#pragma mark   开始扫描
- (void)startScan {
    self.infoView.hidden = YES;
    self.scanBgView.hidden = NO;
   [self.scanView startAnimatian];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.scanView stopAnimation];
        self.scanBgView.hidden = YES;
        [self matchSuccess];
    });
}

- (void)addUserHeadeWithBottom:(CGFloat)bottonSpace leftSpace:(CGFloat)leftSpace {
    UIImageView *headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWIDTH_RATIO*34, kWIDTH_RATIO*34)];
    headerImg.bottom = self.userHeaderImg.top - kWIDTH_RATIO*bottonSpace;
    headerImg.left = self.userHeaderImg.left - kWIDTH_RATIO*leftSpace;

    headerImg.image = [UIImage imageNamed:@"pk_userHeader"];
    headerImg.contentMode = UIViewContentModeScaleAspectFill;
    [headerImg js_setRoundedCorners:UIRectCornerAllCorners radii:kWIDTH_RATIO*17 borderColor:UIColor.whiteColor width:0.5];
    
    [self.scanBgView addSubview:headerImg];
}


#pragma mark   匹配到人
- (PKMatchView *)matchView {
    if (!_matchView) {
        _matchView = [[PKMatchView alloc]initWithFrame:CGRectMake(0, NavigationBarHeight, SCREEN_W, SCREEN_H - NavigationBarHeight)];
        _matchView.hidden = YES;
    }
    return _matchView;
}
- (void)matchSuccess {
    self.matchView.hidden = NO;
    [self.matchView startAnimation];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.matchView.hidden = YES;
        self.datiView.hidden = NO;
        [self.datiView startDati];
    });
}
- (PKDatiView *)datiView {
    if (!_datiView) {
        _datiView = [[PKDatiView alloc]initWithFrame:self.matchView.frame];
        _datiView.hidden = YES;
        WS(WeakSelf)
        _datiView.resultBlock = ^(NSInteger mytime, NSInteger otherTime, NSArray * _Nonnull myArray, NSArray * _Nonnull otherArray) {
            WeakSelf.datiView.hidden = YES;
            WeakSelf.resultView.hidden = NO;
            [WeakSelf.resultView updateUIWithMytime:mytime otherTime:otherTime myArray:myArray otherArray:otherArray];
        };
    }
    return _datiView;
}



- (PKResultView *)resultView {
    if (!_resultView) {
        _resultView = [[PKResultView alloc]initWithFrame:self.matchView.frame];
        _resultView.hidden = YES;
        WS(WeakSelf)
        _resultView.againBlock = ^{
            WeakSelf.resultView.hidden = YES;
            WeakSelf.infoView.hidden = NO;
            [WeakSelf.infoView requestData];
        };
    }
    return _resultView;
}
@end
